unpartial
Unpartial a type.
It is very common to define a config type and Partial<>
it in a function argument.
When we received the argument, we want to merge it with our default config before using it:
import { unpartial } from 'unpartial'
interface Config {
require: { a: number }
optional?: { a: number }
}
const defaultConfig = { require: { a: 1 } }
function foo(givenConfig?: Partial<Config>) {
const config = unpartial(defaultConfig, givenConfig);
}
Code completion is available as you type:
const config = unpartial(defaultConfig, { });
It also supports merging two default configs.
This is useful when you are extending the interface from another package/class.
import { unpartial } from 'unpartial'
import { Option, defaultOption } from 'another-package'
interface MyOption extends Option { ... }
const myDefaultOption = { ... }
function foo(givenOption?: Partial<MyOption>) {
const option = unpartial(defaultOption, myDefaultOption, givenOption)
}
There are 3 more functions available in this library:
unpartialRecursively()
: unpartial()
deeply.required()
: an improved version of unpartial()
with better type management.requiredDeep()
: an improved version of unpartialRecursively()
with better type management.
The required()
and requiredDeep()
function is also exposed in type-plus
.
Feel free to check it out.
Contribute
yarn
git checkout -b <branch>
yarn watch
git commit -m "<commit message>"
git push